/*
 * BangoStorage.h
 *
 *  Created on: 7 Jan 2013
 */

#ifndef BANGOSTORAGE_H_
#define BANGOSTORAGE_H_


#include "BangoConstants.h"
#include "BangoProfile.h"
#include "BangoEvent.h"

#include <bb/data/SqlDataAccess>
#include <bb/data/DataSource>
#include <bb/data/SqlConnection>
#include <QtSql>
#include <iostream>
#include <QMutex>
#include <QList>


using namespace bb::data;
using namespace std;

namespace bb
{
	namespace data
	{
		class SqlConnection;
	}
}

class BangoStorage : public QObject
{
	Q_OBJECT

private:
	QSqlDatabase dbBango;
	int _profileId;
	bool _persistDatabaseConnection;
	bool _openLogProfile;
	bool _openUpdateProfileLocation;
	bool _openLogEvent;
	bool _openProfilesForPost;
	bool _openEventsForPost;
	bool _openEventsPosted;
	bool _openDeleteEventsPosted;

	void CloseConnection();
	bool CopyDbToDataFolder();

	QString ProfileTableQuery();
	QString EventTableQuery();
	QString EventParameterTableQuery();
	void CreateTables();
	void DropTables();
	void CompactDatabase() const;
	QString CompileQueryStatement(const QString tableName, const QString queryText);
	void LogEventParameter(int eventId, QVector<BangoNameValuePair> eventParameters);
	bool LoadDatabase();
	SqlConnection *_sqlConnection;
	QString _databaseNameWithPath;
	static int const LOAD_EXECUTION;
	QString _databaseName;
	int _databaseVersion;


public:
	BangoStorage(QObject *parent = 0);
	~BangoStorage();

	QString CompileCreateTableQuery(const QString tablename, const QString queryText);

	bool PersistDatabaseConnection(){return _persistDatabaseConnection;};
	void PersistDatabaseConnection(bool persistDatabaseConnection){_persistDatabaseConnection = persistDatabaseConnection;}
	int ProfileId(){return _profileId;}
	void ProfileId(int profileId){_profileId = profileId;}
	void LogProfile(BangoProfile bangoProfile);
	void LogProfile(int sdkType, QString sdkVersion, QString applicationId, QString applicationType,
			QString applicationChannel, QString applicationVersion, QString userId, int sessionStartTime,int connectionType, bool isRoaming,
			QString locationSource, double locationAltitude, double locationLatitude, double locationLongitude, float locationAccuracy, QString mOperator, QString operatorName,
			QString operatorCountryIso, QString simOperator, QString simCardSerial, QString device, QString model, QString hardwareId,
			QString version, QString pin, QString operatorConnectionType);
	void UpdateProfileLocation(QString locationSource, double locationLatitude, double locationLongitude, double locationAltitude, float locationAccuracy);
	int LogEvent(BangoEvent bangoEvent);
	int LogEvent(int connectionType, QString operatorConnectionType, int errorLevel, QString eventName, QString eventDetail, QString eventValue, QString errorId, QDateTime eventTime, QDateTime eventLocalTime, QVector<BangoNameValuePair> eventParameters);
	QVector<BangoProfile> GetProfilesForPost(bool notCurrentProfile);
	QVector<BangoEvent> GetEventsForPost(int profileId);
	bool DeleteOrphannedRecords();
	bool UpdateEventsPosted(int profileId);
	bool DeleteEventsPosted(int profileId);
	void CloseDatabase(bool ignorePersistConnection);

};

#endif /* BANGOSTORAGE_H_ */
